﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WCSY.BLL;
using WCSY.MODEL;

public partial class AuxiliaryManagement_BasicDataManagement_ApprovFlow_ApprovalFlow : System.Web.UI.Page
{

    fg_approveFlowMstAppManager appmstm = new fg_approveFlowMstAppManager();
    fg_approveFlowDetAppManager appdetm = new fg_approveFlowDetAppManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sysno = Request["sysno"];
            if (sysno != null)
            {
                //修改状态
                bindInfo();
                //设置所有控件为只读属性
                tool.SetControlReadOnly(this);
                //设置保存按钮为隐藏状态
                this.btnSave.Visible = false;

            }
            else {
                //新建状态
                //初始化主表信息
                this.txtsysno.Text = tool.GetMaxSysno("sysno", "fg_approveFlowMstApp");
                this.txtfilldate.Text = DateTime.Now.Date.ToShortDateString();
                if (Session["userName"] == null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>chkLogin();</script>");
                }
                this.txtfillPsn.Text = Session["userName"].ToString();

                //绑定业务对象
                fg_operatesAppManager oappm = new fg_operatesAppManager();
                DataTable opDt = oappm.GetList(" isApp = 1 and (isDefaultApp =0 or isDefaultApp is null)").Tables[0];
                this.ddlopNo.DataSource = opDt;
                this.ddlopNo.DataTextField = "opName";
                this.ddlopNo.DataValueField = "opNo";
                this.ddlopNo.DataBind();


                //初始明细信息
                this.Repeater1.DataSource = tool.GetAppOrderDetData();
                this.Repeater1.DataBind();
            }
           
        }
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sysno = Request["sysno"];
        if (sysno != null)
        {
            //修改状态
            List<string> sqlList = new List<string>();

            #region 修改主表数据
            try
            {
                fg_approveFlowMstApp mst = new fg_approveFlowMstApp();
                mst.sysno = sysno;
                mst.approvName = txtapprovName.Text;
                mst.opNo = ddlopNo.SelectedValue;
                mst.fillPsn = txtfillPsn.Text;
                mst.filldate = Convert.ToDateTime(txtfilldate.Text);
                mst.remarks = txtremarks.Text;
                sqlList.Add(appmstm.Update(mst));
            }
            catch (Exception ex)
            {
                tool.Alert("修改失败！" + ex.Message, this);
                return;
            }
            #endregion

            #region 保存明细数据
            try
            {
                sqlList.Add(appdetm.Delete(sysno));
                fg_approveFlowDetApp det = null;
                for (int i = 0; i < Convert.ToInt32(HiddenField1.Value); i++)
                {
                    det = new fg_approveFlowDetApp();
                    det.sysno = sysno;
                    det.lineid = Convert.ToInt32((this.Repeater1.Items[i].FindControl("lbllineid") as Label).Text);
                    det.ordNo = Convert.ToInt32((this.Repeater1.Items[i].FindControl("txtordNo") as TextBox).Text.Equals("") ? "0" : (this.Repeater1.Items[i].FindControl("txtordNo") as TextBox).Text);
                    //判断顺序号是否填写，如果没有填写，则跳过该行，继续循环后面的行。
                    if (det.ordNo == 0)
                    {
                        continue;
                    }
                    det.approvPost = (this.Repeater1.Items[i].FindControl("txtapprovPostNo") as HiddenField).Value;
                    CheckBox chkisSuPost = this.Repeater1.Items[i].FindControl("chkisSuPost") as CheckBox;
                    if (chkisSuPost.Checked)
                    {
                        det.isSuPost = "1";
                    }
                    else
                    {
                        det.isSuPost = "0";
                    }
                    //判断如果有顺序号，审批岗位或上级岗位必须填写一个，否则进行提示，设置出错。
                    if (det.approvPost.Equals("") && det.isSuPost.Equals("0"))
                    {
                        throw new Exception("第"+det.lineid+"行，审批岗位和上级岗位选项，不能同时为空，请设置审批岗位或设置上级岗位！");
                    }
                    det.approvPercent = Convert.ToInt32((this.Repeater1.Items[i].FindControl("txtapprovPercent") as TextBox).Text.Equals("") ? "0" : (this.Repeater1.Items[i].FindControl("txtapprovPercent") as TextBox).Text);
                    det.remarks = (this.Repeater1.Items[i].FindControl("txtremarks") as TextBox).Text;
                    sqlList.Add(appdetm.Add(det));

                }
            }
            catch (Exception ex)
            {
                tool.Alert("修改失败！" + ex.Message, this);
                return;
            }
            #endregion

            try
            {
                string suc = tool.executeSqlTran(sqlList);
                if (!suc.Equals("suc"))
                {
                    throw new Exception(suc);
                }
            }
            catch (Exception ex)
            {

                tool.Alert("信息修改失败!" + ex.Message, this);
                return;
            }
        }
        else
        {
            sysno = tool.GetMaxSysno("sysno", "fg_approveFlowMstApp");
            List<string> sqlList = new List<string>();

            #region 保存主表数据
            try
            {
                fg_approveFlowMstApp mst = new fg_approveFlowMstApp();
                mst.sysno = sysno;
                mst.approvName = txtapprovName.Text;
                mst.opNo = ddlopNo.SelectedValue;
                mst.fillPsn = txtfillPsn.Text;
                mst.filldate = Convert.ToDateTime(txtfilldate.Text);
                mst.remarks = txtremarks.Text;
                sqlList.Add(appmstm.Add(mst));
            }
            catch (Exception ex)
            {
                tool.Alert("保存失败！" + ex.Message, this);
                return;
            }
            #endregion

            #region 保存明细数据
            try
            {
                fg_approveFlowDetApp det = null;
                for (int i = 0; i < Convert.ToInt32(HiddenField1.Value); i++)
                {
                    det = new fg_approveFlowDetApp();
                    det.sysno = sysno;
                    det.lineid = Convert.ToInt32((this.Repeater1.Items[i].FindControl("lbllineid") as Label).Text);
                    det.ordNo = Convert.ToInt32((this.Repeater1.Items[i].FindControl("txtordNo") as TextBox).Text.Equals("") ? "0" : (this.Repeater1.Items[i].FindControl("txtordNo") as TextBox).Text);
                    //判断顺序号是否填写，如果没有填写，则跳过该行，继续循环后面的行。
                    if (det.ordNo == 0)
                    {
                        continue;
                    }
                    det.approvPost = (this.Repeater1.Items[i].FindControl("txtapprovPostNo") as HiddenField).Value;
                    CheckBox chkisSuPost = this.Repeater1.Items[i].FindControl("chkisSuPost") as CheckBox;
                    if (chkisSuPost.Checked)
                    {
                        det.isSuPost = "1";
                    }
                    else
                    {
                        det.isSuPost = "0";
                    }
                    //判断如果有顺序号，审批岗位或上级岗位必须填写一个，否则进行提示，设置出错。
                    if (det.approvPost.Equals("") && det.isSuPost.Equals("0"))
                    {
                        throw new Exception("第" + det.lineid + "行，审批岗位和上级岗位选项，不能同时为空，请设置审批岗位或设置上级岗位！");
                    }
                    det.approvPercent = Convert.ToInt32((this.Repeater1.Items[i].FindControl("txtapprovPercent") as TextBox).Text.Equals("") ? "0" : (this.Repeater1.Items[i].FindControl("txtapprovPercent") as TextBox).Text);
                    det.remarks = (this.Repeater1.Items[i].FindControl("txtremarks") as TextBox).Text;
                    sqlList.Add(appdetm.Add(det));

                }
            }
            catch (Exception ex)
            {
                tool.Alert("保存失败！" + ex.Message, this);
                return;
            }
            #endregion

            try
            {
                string suc = tool.executeSqlTran(sqlList);
                if (!suc.Equals("suc"))
                {
                    throw new Exception(suc);
                }
            }
            catch (Exception ex)
            {

                tool.Alert("信息保存失败!" + ex.Message, this);
                return;
            }
        }
        
        tool.Alert("信息保存成功!", this);
    }

    /// <summary>
    ///绑定历史数据 
    /// </summary>
    public void bindInfo() { 
        
        string sysno = Request["sysno"].ToString();
        //绑定业务对象
        fg_operatesAppManager oappm = new fg_operatesAppManager();
        DataTable opDt = oappm.GetList(" isApp = 1 ").Tables[0];
        this.ddlopNo.DataSource = opDt;
        this.ddlopNo.DataTextField = "opName";
        this.ddlopNo.DataValueField = "opNo";
        this.ddlopNo.DataBind();
        //绑定主表
        this.txtsysno.Text = sysno;
        fg_approveFlowMstApp mstapp = appmstm.GetModel(sysno);
        if(mstapp!=null){
            this.txtapprovName.Text = mstapp.approvName;
            this.ddlopNo.SelectedValue = mstapp.opNo;
            this.txtfillPsn.Text = mstapp.fillPsn;
            this.txtfilldate.Text = Convert.ToDateTime(mstapp.filldate).ToShortDateString();
            this.txtremarks.Text = mstapp.remarks;
        }
        //绑定明细表
       DataTable appdetDt = appdetm.GetList("sysno = '"+sysno+"' order by lineid").Tables[0];
        //不足10行补齐10行
       int k = appdetDt.Rows.Count;
       for (int i = 1; i <= 10 - k;i++ )
       {
           DataRow dr = appdetDt.NewRow();
           dr["lineid"] = i + k;
           dr["issuPost"] = 0;
           appdetDt.Rows.Add(dr);
       }
       this.Repeater1.DataSource = appdetDt;
       this.Repeater1.DataBind();
       
    }
    //点击修改按钮，设置界面属性
    protected void btnUpd_Click(object sender, EventArgs e)
    {
        //设置所有控件取消只读状态
        tool.SetControlReadOnlyNone(this);
        //设置sysno为只读属性
        tool.SetControlReadOnly(txtsysno);
        //设置保存按钮可访问，并隐藏修改按钮
        this.btnSave.Visible = true;
        this.btnUpd.Visible = false;
    }
}
